package com.example.foodisimportant.RegisterActivity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.foodisimportant.utils.DBUtil;
import com.example.foodisimportant.LoginActivity.LoginActivity;
import com.example.foodisimportant.R;
import com.example.foodisimportant.StudentViewActivity.StudentViewActivity;

import java.sql.SQLException;

public class RegistActivity extends AppCompatActivity {

    private EditText mETName,mETNumber,mETPwd,mETConfirmPwd;
    private TextView mBtnSummit;
    private RadioGroup mRgcampus;
    private RadioButton mRbcampus;
    private String id,name,pwd,confirmpwd,campus;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        init();
    }

    private void init(){
        //与控件相对应
        mBtnSummit=findViewById(R.id.btn_summit);
        mETNumber=findViewById(R.id.et_id);
        mETName=findViewById(R.id.et_name);
        mETPwd=findViewById(R.id.et_pwd);
        mETConfirmPwd=findViewById(R.id.et_confirmpwd);
        mRgcampus=findViewById(R.id.rdgroup_position);
        mRbcampus=findViewById(R.id.rbg1);
        mRgcampus.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                mRbcampus=findViewById(group.getCheckedRadioButtonId());
            }
        });

        //子线程
        WorkThread wt2=new WorkThread();
        wt2.start();

        //提交注册信息
        mBtnSummit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取输入的值
                id=mETNumber.getText().toString();
                name=mETName.getText().toString();
                pwd=mETPwd.getText().toString();
                confirmpwd=mETConfirmPwd.getText().toString();
                campus=mRbcampus.getText().toString();
                //判断输入是否符合标准,不符合则弹窗使之重新输入
                if(name.length() == 0){
                    Toast.makeText(RegistActivity.this,"用户名不能为空",Toast.LENGTH_SHORT).show();
                }
                else if(4 >= name.length()){
                    Toast.makeText(RegistActivity.this,"用户名长度应在4-18字之间",Toast.LENGTH_SHORT).show();
                }
                else if(name.length() >= 18){
                    Toast.makeText(RegistActivity.this,"用户名长度应在4-18字之间",Toast.LENGTH_SHORT).show();
                }
                else if(name.length() >= 18){
                    Toast.makeText(RegistActivity.this,"用户名长度应在4-18字之间",Toast.LENGTH_SHORT).show();
                }
                //这里比较两个密码是否相同，不能用== 或者!=来判断string，string是引用类型的
                //不是基本数据类型，所以它们比较使用地址和值（相当于C的指针）
                //来比较的，所以 str == str2永远是false，那么就应该将Text取出比较
                //getText得到内容
                //toString以str形式
                //trim可以删去前后的空格
                //如果两部分相等则等于1，注册成功，关掉页面回到登陆页面
                else if(pwd.equals(confirmpwd)){
                    //进行数据的传递
                    Message m= handler.obtainMessage();
                    Bundle b=new Bundle();
                    b.putString("id",id);
                    b.putString("name",name);
                    b.putString("pass",pwd);
                    b.putString("campus",campus);
                    m.setData(b);
                    handler.sendMessage(m);
                }
                else {
                    Toast.makeText(RegistActivity.this,"两次输入的密码不一致",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    class WorkThread extends Thread {
        @Override
        public void run() {
            Looper.prepare();
            handler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    Bundle b = msg.getData();//得到与信息对用的Bunlde，Bunlde的作用是在Activity间传递数据
                    DBUtil db = new DBUtil(id, name, pwd, campus); //调用数据库查询类
                    boolean ret = Boolean.parseBoolean(null); //得到返回值
                    try {
                        ret = db.InsertSQL(id, name, pwd, campus);
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                    if (ret == true) {
                        Toast.makeText(RegistActivity.this, "恭喜您注册成功！一起做好这件天大的事吧！", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(RegistActivity.this, StudentViewActivity.class);
                        finish();
                        startActivity(intent);
                        return;
                    } else {
                        Toast.makeText(RegistActivity.this, "注册失败！", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(RegistActivity.this, LoginActivity.class);
                        startActivity(intent);
                    }
                }
            };
            Looper.loop();//Looper循环，通道中有数据执行，无数据堵塞
        }
    }
}